package cn.shop.front.service.impl;

import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.base.MyServiceImpl;
import cn.shop.front.service.AlipayService;
import cn.shop.transcation.alipay.entity.AlipayAccount;
import cn.shop.transcation.alipay.mapper.AlipayAccountMapper;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AlipayServiceImpl extends MyServiceImpl<AlipayAccountMapper, AlipayAccount> implements AlipayService {


    @Override
    public long getAliAccountCount(long uid) {
        return count(Wrappers.<AlipayAccount>lambdaQuery()
                .eq(AlipayAccount::getOwnerId, uid));
    }

    @Override
    public List<Map<String, Object>> aliAccountList(long uid) {
        List<AlipayAccount> list = list(Wrappers.<AlipayAccount>lambdaQuery()
                .eq(AlipayAccount::getOwnerId, uid));
        return list.stream().map(this::formant).collect(Collectors.toList());
    }

    private Map<String, Object> formant(AlipayAccount account) {
        String str = "";
        if (PhoneUtil.isMobile(account.getAccount())) {
            str = DesensitizedUtil.mobilePhone(account.getAccount());
        } else if (ReUtil.isMatch(RegexPool.EMAIL,account.getAccount())) {
            str = DesensitizedUtil.email(account.getAccount());
        } else {
            str = StrUtil.hide(account.getAccount(), 2, account.getAccount().length());
        }
        return Putif.start()
                .anyway("account", str)
                .anyway("name", DesensitizedUtil.chineseName(account.getName()))
                .anyway("id", account.getId())
                .get();
    }
}
